package com.zte.cl.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.zte.cl.model.News;

@Repository
public interface NewsRepository extends JpaRepository<News, String>, JpaSpecificationExecutor<News> {

	long countByChannelIdAndContent(String channelId, String content);
	
	long countByChannelIdAndPageUrl(String channelId, String pageUrl);
	
	long countByChannelIdAndTitle(String channelId, String title);
	
	List<News> findByIsEnabledFalseAndInsertTimeGreaterThan(Date currentTime);
	
	List<News> findByIsEnabledTrueOrderByPublishTimeDesc();
	
	List<News> findByIsEnabledTrueAndKeywordLikeOrderByPublishTimeDesc(String keyword);
	
	List<News> findByIsEnabledFalse();
	
	@Transactional
	@Modifying
	@Query("delete from News n where n.isEnabled = 0")
	List<String> deleteByIsEnabledFalse();
	
	@Query("select n.id from News n where n.pageId in (?1)")
	List<String> findIdByPageId(List<String> pageIdList);
	
	@Transactional
	@Modifying
	@Query("delete from News n where n.id in (?1)")
	void deleteByIdIn(List<String> idList);
	
	List<News> findByPageIdIn(List<String> pageIdList);
}
